home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000030.txt < prev    next >
Text File  |  2013-04-03  |  8KB  |  203 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the extension API.
  6.  
  7. var extensionNatives = requireNative('extension');
  8. var GetExtensionViews = extensionNatives.GetExtensionViews;
  9. var OpenChannelToExtension = extensionNatives.OpenChannelToExtension;
  10. var OpenChannelToNativeApp = extensionNatives.OpenChannelToNativeApp;
  11.  
  12. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  13.  
  14. var inIncognitoContext = requireNative('process').InIncognitoContext();
  15. var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
  16. var contextType = requireNative('process').GetContextType();
  17.  
  18. chrome.extension = chrome.extension || {};
  19.  
  20. var manifestVersion = requireNative('process').GetManifestVersion();
  21. if (manifestVersion < 2) {
  22.   chrome.self = chrome.extension;
  23.   chrome.extension.inIncognitoTab = inIncognitoContext;
  24. }
  25.  
  26. chrome.extension.inIncognitoContext = inIncognitoContext;
  27.  
  28. // This should match chrome.windows.WINDOW_ID_NONE.
  29. //
  30. // We can't use chrome.windows.WINDOW_ID_NONE directly because the
  31. // chrome.windows API won't exist unless this extension has permission for it;
  32. // which may not be the case.
  33. var WINDOW_ID_NONE = -1;
  34.  
  35. chromeHidden.registerCustomHook('extension',
  36.                                 function(bindingsAPI, extensionId) {
  37.   var apiFunctions = bindingsAPI.apiFunctions;
  38.  
  39.   apiFunctions.setHandleRequest('getViews', function(properties) {
  40.     var windowId = WINDOW_ID_NONE;
  41.     var type = 'ALL';
  42.     if (properties) {
  43.       if (properties.type != null) {
  44.         type = properties.type;
  45.       }
  46.       if (properties.windowId != null) {
  47.         windowId = properties.windowId;
  48.       }
  49.     }
  50.     return GetExtensionViews(windowId, type) || null;
  51.   });
  52.  
  53.   apiFunctions.setHandleRequest('getBackgroundPage', function() {
  54.     return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
  55.   });
  56.  
  57.   apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
  58.     if (windowId == null)
  59.       windowId = WINDOW_ID_NONE;
  60.     return GetExtensionViews(windowId, 'TAB');
  61.   });
  62.  
  63.   apiFunctions.setHandleRequest('getURL', function(path) {
  64.     path = String(path);
  65.     if (!path.length || path[0] != '/')
  66.       path = '/' + path;
  67.     return 'chrome-extension://' + extensionId + path;
  68.   });
  69.  
  70.   function sendMessageUpdateArguments(functionName) {
  71.     // Align missing (optional) function arguments with the arguments that
  72.     // schema validation is expecting, e.g.
  73.     //   extension.sendRequest(req)     -> extension.sendRequest(null, req)
  74.     //   extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb)
  75.     var args = Array.prototype.splice.call(arguments, 1);  // skip functionName
  76.     var lastArg = args.length - 1;
  77.  
  78.     // responseCallback (last argument) is optional.
  79.     var responseCallback = null;
  80.     if (typeof(args[lastArg]) == 'function')
  81.       responseCallback = args[lastArg--];
  82.  
  83.     // request (second argument) is required.
  84.     var request = args[lastArg--];
  85.  
  86.     // targetId (first argument, extensionId in the manfiest) is optional.
  87.     var targetId = null;
  88.     if (lastArg >= 0)
  89.       targetId = args[lastArg--];
  90.  
  91.     if (lastArg != -1)
  92.       throw new Error('Invalid arguments to ' + functionName + '.');
  93.     return [targetId, request, responseCallback];
  94.   }
  95.   apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
  96.       sendMessageUpdateArguments.bind(null, 'sendRequest'));
  97.   apiFunctions.setUpdateArgumentsPreValidate('sendMessage',
  98.       sendMessageUpdateArguments.bind(null, 'sendMessage'));
  99.   apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage',
  100.       sendMessageUpdateArguments.bind(null, 'sendNativeMessage'));
  101.  
  102.   apiFunctions.setHandleRequest('sendRequest',
  103.                                 function(targetId, request, responseCallback) {
  104.     if (sendRequestIsDisabled)
  105.       throw new Error(sendRequestIsDisabled);
  106.     var port = chrome.extension.connect(targetId || extensionId,
  107.                                         {name: chromeHidden.kRequestChannel});
  108.     chromeHidden.Port.sendMessageImpl(port, request, responseCallback);
  109.   });
  110.  
  111.   if (sendRequestIsDisabled) {
  112.     chrome.extension.onRequest.addListener = function() {
  113.       throw new Error(sendRequestIsDisabled);
  114.     }
  115.     if (contextType == 'BLESSED_EXTENSION') {
  116.       chrome.extension.onRequestExternal.addListener = function() {
  117.         throw new Error(sendRequestIsDisabled);
  118.       }
  119.     }
  120.   }
  121.  
  122.   apiFunctions.setHandleRequest('sendMessage',
  123.                                 function(targetId, message, responseCallback) {
  124.     var port = chrome.extension.connect(targetId || extensionId,
  125.                                         {name: chromeHidden.kMessageChannel});
  126.     chromeHidden.Port.sendMessageImpl(port, message, responseCallback);
  127.   });
  128.  
  129.   apiFunctions.setHandleRequest('sendNativeMessage',
  130.                                 function(targetId, message, responseCallback) {
  131.     var port = chrome.extension.connectNative(
  132.         targetId, message, chromeHidden.kNativeMessageChannel);
  133.     chromeHidden.Port.sendMessageImpl(port, '', responseCallback);
  134.   });
  135.  
  136.   apiFunctions.setUpdateArgumentsPreValidate('connect', function() {
  137.     // Align missing (optional) function arguments with the arguments that
  138.     // schema validation is expecting, e.g.
  139.     //   extension.connect()   -> extension.connect(null, null)
  140.     //   extension.connect({}) -> extension.connect(null, {})
  141.     var nextArg = 0;
  142.  
  143.     // targetId (first argument) is optional.
  144.     var targetId = null;
  145.     if (typeof(arguments[nextArg]) == 'string')
  146.       targetId = arguments[nextArg++];
  147.  
  148.     // connectInfo (second argument) is optional.
  149.     var connectInfo = null;
  150.     if (typeof(arguments[nextArg]) == 'object')
  151.       connectInfo = arguments[nextArg++];
  152.  
  153.     if (nextArg != arguments.length)
  154.       throw new Error('Invalid arguments to connect.');
  155.     return [targetId, connectInfo];
  156.   });
  157.  
  158.   apiFunctions.setUpdateArgumentsPreValidate('connectNative', function() {
  159.     var nextArg = 0;
  160.  
  161.     // appName is required.
  162.     var appName = arguments[nextArg++];
  163.  
  164.     // connectionMessage is required.
  165.     var connectMessage = arguments[nextArg++];
  166.  
  167.     // channelName is only passed by sendMessage
  168.     var channelName = 'connectNative';
  169.     if (typeof(arguments[nextArg]) == 'string')
  170.       channelName = arguments[nextArg++];
  171.  
  172.     if (nextArg != arguments.length)
  173.       throw new Error('Invalid arguments to connectNative.');
  174.     return [appName, {name: channelName, message: connectMessage}];
  175.   });
  176.  
  177.   apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) {
  178.     if (!targetId)
  179.       targetId = extensionId;
  180.     var name = '';
  181.     if (connectInfo && connectInfo.name)
  182.       name = connectInfo.name;
  183.  
  184.     var portId = OpenChannelToExtension(extensionId, targetId, name);
  185.     if (portId >= 0)
  186.       return chromeHidden.Port.createPort(portId, name);
  187.     throw new Error('Error connecting to extension ' + targetId);
  188.   });
  189.  
  190.   apiFunctions.setHandleRequest('connectNative',
  191.                                 function(nativeAppName, connectInfo) {
  192.     // Turn the object into a string here, because it eventually will be.
  193.     var portId = OpenChannelToNativeApp(extensionId,
  194.                                         nativeAppName,
  195.                                         connectInfo.name,
  196.                                         JSON.stringify(connectInfo.message));
  197.     if (portId >= 0) {
  198.       return chromeHidden.Port.createPort(portId, connectInfo.name);
  199.     }
  200.     throw new Error('Error connecting to native app: ' + nativeAppName);
  201.   });
  202. });
  203.